﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLBH.Base;
using QLBH.StandardUI;
using QLBH.DataModel;
using QLBH.Model.Controller;
using QLBH.ModelInterface;

namespace QLBH.Category
{
    public partial class AddKhoDlg : Form
    {
        public eDlgType DlgType { set; get; }
        private IQLKhoModel model;

        public IQLKhoModel Model
        {
            get { return model; }
            set { model = value; }
        }
        public AddKhoDlg()
        {
            InitializeComponent();
            DlgType = eDlgType.ADD;
            model = ModelFactory.ModelFactory.GetQLKhoModelInstance();

            this.KeyPreview = true;
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (UpdateModelFromControl()==false)
                {
                    return;
                }
                
                KHO entity = new KHO();
                entity.DIACHI = model.DiaChi;
                entity.GHICHU = model.GhiChu;
                entity.KHOCODE = model.MaKho;
                entity.MACDINH = (bool)model.MacDinh;
                entity.TENKHO = model.TenKho;
                entity.KHOID = model.KhoID;
                if (DlgType == eDlgType.ADD)
                {
                    entity.KHOID = Guid.NewGuid();
                    KhoController.GetInstance().Add(entity);
                }
                else
                {
                    KhoController.GetInstance().Edit(entity);
                }
                DialogResult = DialogResult.OK;
                this.Close();
            }
            catch (System.Exception ex)
            {
                ShowMessage.Instance.ShowError(ex.Message);
            }
            
        }

        private void AddKhoDlg_Load(object sender, EventArgs e)
        {
            if (DlgType==eDlgType.EDIT)
            {
                UpdateModelToControl();
                
            }
            else
            {
                tbxMaKho.Text = CodeHelper.GetNewCode();                
            }
        }
        private void UpdateModelToControl()
        {
            tbxMaKho.Text = model.MaKho;
            tbxTenKho.Text = model.TenKho;
            rtbDiachi.Text = model.DiaChi;
            rtbGhiChu.Text = model.GhiChu;
            chbDefault.Checked = model.MacDinh;
        }
        private bool UpdateModelFromControl()
        {
            if (CheckDataInput()==false)
            {
                return false;
            }
            
            model.MaKho = tbxMaKho.Text;
            model.TenKho = tbxTenKho.Text;
            model.DiaChi = rtbDiachi.Text;
            model.GhiChu = rtbGhiChu.Text;
            model.MacDinh = chbDefault.Checked;

            return true;
        }
        private bool CheckDataInput()
        {
            try
            {
                DataInputHelper.CheckTextData(tbxTenKho.Text, "Tên Kho",50);
                return true;
            }
            catch (System.Exception ex)
            {
                ShowMessage.Instance.ShowError(ex.Message);
                tbxTenKho.SelectAll();
                tbxTenKho.Focus();
                return false;
            }
                         
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void AddKhoDlg_KeyPress(object sender, KeyPressEventArgs e)
        {
           
        }

        private void AddKhoDlg_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode==Keys.Escape)
            {
                this.DialogResult = DialogResult.Cancel;
                this.Close();
            }
            else if (e.Control && e.KeyCode==Keys.S)
            {
                //Ctrl+S
                btnSave_Click(sender, e);
            }
        }
    }
}
